﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.SecurityEvaluate;

public partial class Police_SecurityEvaluate_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDepartment();
        }
    }

    void BindDepartment()
    {
        dptDepartment.DataSource = Yipusoft.Account.BLL.Department.GetList("Right(DepartmentID,3)='000' and DepartmentID<>'1300000'");
        dptDepartment.DataTextField = "FullName";
        dptDepartment.DataValueField = "DepartmentID";
        dptDepartment.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string where = " departmentId like '" + dptDepartment.SelectedValue.Substring(0, 4) + "3%' ";
        List<DepartmentInfo> Departments = Department.GetList(where);
        foreach (DepartmentInfo info in Departments)
        {
            string DepartmentID = info.DepartmentID;
            DateTime StartTime = Convert.ToDateTime("2009-9-28");
            DateTime EndTime = Convert.ToDateTime("2009-10-5");
            CarOrder order = Park.GetParkLog2(StartTime, EndTime, DepartmentID);
            if (order != null)
            {
                ParkLogs park = Park.GetParkLog(StartTime, EndTime, DepartmentID);
                if (park == null)
                {
                    ParkLogDataContext pldc = new ParkLogDataContext();
                    //签字
                    ParkLogs logQZ = pldc.ParkLogs.Where(o => o.DepartmentID == DepartmentID && o.Captain != "").OrderByDescending(o => o.Date).FirstOrDefault();

                    park = new ParkLogs();
                    park.Date = order.Date;
                    park.DepartmentID = order.DepartmentID;
                    park.DepartmentName = order.DepartmentName;
                    park.ID = Guid.NewGuid();
                    park.SetDate = order.SetDate;
                    park.SetUsers = order.UserID;
                    if (logQZ != null)
                    {
                        park.Captain = logQZ.Captain;
                    }
                    else
                    {
                        park.Captain = "";
                    }
                    pldc.ParkLogs.InsertOnSubmit(park);


                    List<CarOrder> orders = pldc.CarOrder.Where(o => o.Date == order.Date && o.DepartmentID == DepartmentID).ToList();
                    foreach (CarOrder o in orders)
                    {
                        o.LogID = park.ID;
                    }
                    pldc.SubmitChanges();

                }
                else
                {
                    ParkLogDataContext pldc = new ParkLogDataContext();
                    List<CarOrder> orders = pldc.CarOrder.Where(o => o.Date == order.Date && o.DepartmentID == DepartmentID).ToList();
                    foreach (CarOrder o in orders)
                    {
                        o.LogID = park.ID;
                        pldc.SubmitChanges();
                    }

                }
            }

        }
    }
}
